package com.example.zhu95.mytimber.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.zhu95.mytimber.R;

/**
 * @author zhu95 on 2017/12/13.
 *         http://blog.csdn.net/u014738387/article/details/50235139
 */
public class MusicLinearClip extends View {
    private int mWidth;
    private int mHeight;
    private int mCurrentHeight;
    private int mRectWidth;
    private int mRectCount = 3;
    private Paint mPaint;
    private int offset = 6;//条形之间的间距

    public MusicLinearClip(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getWidth();
        mHeight = getHeight();
        //每个长条的宽度
        mRectWidth = (int) (mWidth / mRectCount);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < mRectCount; i++) {
            mCurrentHeight = (int) (Math.random() * 0.8 * mHeight);
            float left = (float) (mRectWidth * i + offset);
            float top = mCurrentHeight;
            float right = (float) (mRectWidth * (i + 1));
            float bottom = mHeight;
            mPaint.setColor(getResources().getColor(R.color.colorAccent));
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
        postInvalidateDelayed(300);
    }
}
